package com.bobi.tfca.ui.mainnavigation.message.notification

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.lifecycle.ViewModel
import androidx.navigation.findNavController
import com.bobi.tfca.R
import com.bobi.tfca.base.BaseFragment
import com.bobi.tfca.constants.MessageStatusEnum
import com.bobi.tfca.databinding.FragmentNotificationDetailBinding
import com.bobi.tfca.ui.mainnavigation.message.letter.LetterDetailFragmentVM
import com.bobi.tfca.util.LogUtil

class NotificationDetailFragment :
    BaseFragment<FragmentNotificationDetailBinding, NotDetailFragmentVM>(
        FragmentNotificationDetailBinding::inflate,
        NotDetailFragmentVM::class.java,
        true
    ) {
    @SuppressLint("ResourceType", "UseRequireInsteadOfGet")
    override fun initFragment(
        binding: FragmentNotificationDetailBinding,
        viewModel: NotDetailFragmentVM?,
        savedInstanceState: Bundle?
    ) {
        // 设置头部菜单的按钮
        setTopPanelStyle(binding)

        // 设置监听
        viewModel!!.notification.observe(viewLifecycleOwner) {
            binding.notification = it
        }

        if (publicViewModel!!.message.value!!.messageStatus == MessageStatusEnum.RECEIVED.code) {
            publicViewModel!!.newNotificationNum.value = publicViewModel!!.newNotificationNum.value?.minus(1)
        }
    }

    /**
     * 设置顶部面板的样式
     */
    private fun setTopPanelStyle(binding: FragmentNotificationDetailBinding) {
        val topPanel = binding.topPanel
        val leftImage = topPanel.leftImage
        leftImage.setOnClickListener {
            requireActivity().findNavController(R.id.app_nav).navigateUp()
        }
        val rightLayout = topPanel.rightLayout
        rightLayout.visibility = View.GONE
        val title = topPanel.titleText
        title.text = "通知"
    }

    override fun resumeFragment(viewModel: NotDetailFragmentVM?) {
        super.resumeFragment(viewModel)
        // 获取信件的id
        val notificationId = publicViewModel!!.message.value!!.messageId

        viewModel!!.getNotificationById(publicViewModel!!, notificationId, this)
    }
}